@include('layouts.header')
<div class="layui-layout layui-layout-admin">
    @include("layouts.header-menu")
    @include("layouts.menu")

    <div class="layui-body">
        <!-- 内容主体区域 -->
        <div style="padding: 15px;">
            <br>
            <table id="infoList" lay-filter="info_list"></table>
            <script type="text/html" id="currentTableBar">
                <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">删除</a>
            </script>
            <br>
        </div>
    </div>
</div>
@include("layouts.footer")

<script type="text/javascript">
    layui.use(['table', 'element','layer', 'jquery', 'util', 'laytpl'], function() {
        var layer = layui.layer,
            $ = layui.jquery,
            form = layui.form,
            laytpl = layui.laytpl,
            table = layui.table,
            util = layui.util;

        table.render({
                elem: '#infoList'
                ,url: '/shzr/list?news_type=1' //数据接口
                ,limit: 20
                ,limits: [10, 20, 50, 100, 150, 200, 300, 500, 1000]
                ,page: true //开启分页
                ,toolbar: '#toolbarDemo'
                ,defaultToolbar: ['exports']
                ,cols: [[ //表头
                    {field: 'id', title: 'ID', width: 80}
                    ,{field: 'title', title: '标题'}
                    ,{field: 'updated_at', title: '更新时间',  width: 180}
                    ,{field: 'created_at', title: '创建时间', width: 180}
                    ,{title:'操作',  toolbar: '#currentTableBar', width:200, align: "left", fixed: 'right'}
                    ]],
        parseData: function(res){
            return {
                "code": res.code,
                "msg":res.msg,
                "data":res.body['list'],
                "count":res.body['page']
            }
        }
    });

        // 添加
        $("#addInfo").on('click', function(){
            layer.open({
                type: 2,
                title: "新增任务",
                shade: .05,
                shadeClose: !0,
                area: function() {
                    return ['50%', '80%']
                } (),
                offset: function() {
                    return /mobile/i.test(navigator.userAgent) ? "auto": "100px"
                } (),
                skin: "layui-layer-border",
                content: "/air/task/create",
                btn: ["取消"],
                btnAlign: ""
            });
        });

        table.on('tool(info_list)', function(obj){
            var info_id = obj.data.id;
            if (obj.event == 'delete') {
                layer.confirm('您确定要删除吗？', function (index) {
                    layer.load('loading', {time: 10000});
                    $.get('/air/task/delete?info_id='+info_id, function(res){
                        layer.closeAll('loading');
                        if (res.code != 0){
                            layer.msg(res.msg, {icon: 2});
                        }else{
                            layer.msg("删除成功", {icon: 1});
                            setTimeout(function(){window.location.reload()}, 2000);
                        }
                    },'json');
                });
            }else if (obj.event == 'edit') {
                window.location.href = '/shzr/create?info_id='+info_id;
            }
        });

        util.on('lay-on', {
            'task_data': function(){
                layer.tips($(this).data("date"), this, {
                    tips: 1
                });
            }
        });
    });
</script>